package org.wmq.taskschedule.algorithm;

import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

/**
 * 图像二值化算法执行器
 * 实现AlgorithmExecutor接口，用于将图像转换为二值图像
 */
public class ImageBinarizationExecutor implements AlgorithmExecutor {

    private double threshold; // 阈值
    private double maxValue;  // 最大值
    private int thresholdType; // 阈值类型
    private String description; // 算法描述

    /**
     * 默认构造函数，使用默认参数
     */
    public ImageBinarizationExecutor() {
        this.threshold = 127.0;
        this.maxValue = 255.0;
        this.thresholdType = Imgproc.THRESH_BINARY;
        this.description = "图像二值化算法 - 将灰度图像转换为黑白二值图像";
    }

    /**
     * 构造函数，允许自定义阈值参数
     * 
     * @param threshold 阈值
     * @param maxValue 最大值
     * @param thresholdType 阈值类型
     */
    public ImageBinarizationExecutor(double threshold, double maxValue, int thresholdType) {
        this.threshold = threshold;
        this.maxValue = maxValue;
        this.thresholdType = thresholdType;
        this.description = "图像二值化算法 - 将灰度图像转换为黑白二值图像";
    }

    /**
     * 构造函数，允许自定义所有参数
     * 
     * @param threshold 阈值
     * @param maxValue 最大值
     * @param thresholdType 阈值类型
     * @param description 算法描述
     */
    public ImageBinarizationExecutor(double threshold, double maxValue, int thresholdType, String description) {
        this.threshold = threshold;
        this.maxValue = maxValue;
        this.thresholdType = thresholdType;
        this.description = description;
    }

    @Override
    public Mat execute(Mat input) {
        if (input == null || input.empty()) {
            throw new IllegalArgumentException("输入图像不能为空");
        }

        // 创建输出图像
        Mat output = new Mat();

        // 如果输入不是灰度图像，先转换为灰度
        Mat grayImage = input.clone();
        if (input.channels() > 1) {
            Imgproc.cvtColor(input, grayImage, Imgproc.COLOR_BGR2GRAY);
        }

        // 应用二值化算法
        Imgproc.threshold(grayImage, output, threshold, maxValue, thresholdType);

        // 释放中间资源
        if (!grayImage.equals(input)) {
            grayImage.release();
        }

        return output;
    }

    // Getters and setters
    public double getThreshold() {
        return threshold;
    }

    public void setThreshold(double threshold) {
        this.threshold = threshold;
    }

    public double getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(double maxValue) {
        this.maxValue = maxValue;
    }

    public int getThresholdType() {
        return thresholdType;
    }

    public void setThresholdType(int thresholdType) {
        this.thresholdType = thresholdType;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * 设置为自适应阈值二值化
     */
    public void setAdaptiveThreshold() {
        this.thresholdType = Imgproc.THRESH_BINARY;
        this.threshold = 0; // 自适应阈值不需要指定阈值
    }

    /**
     * 设置为Otsu阈值二值化
     */
    public void setOtsuThreshold() {
        this.thresholdType = Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU;
    }
}